home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / icons+tools / associate_v1.5 / source / associate.c < prev    next >
C/C++ Source or Header  |  1996-01-11  |  20KB  |  831 lines

  1. /* Associate (C) 1995 Dominic Clifton - AKA Hydra/LSD */
  2.  
  3. // note: you MUST have a global define KS20 or KS30 set or CRASH!!!
  4. // I use SCOptions to set it...
  5.  
  6.  
  7. #define MAIN
  8. #include "includes.h"
  9. static UBYTE   *VersTag = "$VER: Associate 1.5 (13.01.1996)";
  10. #include "vars.h"
  11. #include <wb2cli.h>
  12.  
  13. char *iconname="Progdir:Associate";
  14. char msgstr[300];
  15. BOOL done=FALSE;
  16. char cfgname[]="S:Associate.cfg";
  17. BOOL All,ttall,AllThis;
  18. LONG ttallval;
  19. ULONG doalldrawers=0;
  20. struct TypeNode *MatchType;
  21. LONG IconsToProcess;
  22.  
  23. /*
  24.    SHITTTTTTTTTTTTTTTTTTTTTT Nodes in a list that point to lists that point to
  25.    more nodes are VERY confusing! :-)
  26. */
  27.  
  28. // next four functions nicked from YAK source code..  ta very much..
  29. // (but they are modified a bit tho..)
  30.  
  31. char __regargs *
  32. TTString(struct DiskObject *diskobj,char *name, char *def)
  33. {
  34.   char *what;
  35.   if (diskobj)
  36.     if (what = FindToolType(diskobj->do_ToolTypes, name))
  37.       return what;
  38.   return def;
  39. }
  40.  
  41. /* like ArgInt() */
  42. LONG __regargs
  43. TTInt(struct DiskObject *diskobj,char *name, LONG def)
  44. {
  45.   char *what;
  46.   if (diskobj)
  47.     if (what = FindToolType(diskobj->do_ToolTypes, name))
  48.       StrToLong(what, &def);
  49.   return def;
  50. }
  51.  
  52. struct Node *GetNode(struct List *lh, UWORD n)
  53. {
  54.   struct Node *ln;
  55.  
  56.   for (ln = lh->lh_Head; n--; ln = ln->ln_Succ)
  57.     ;
  58.   return ln;
  59. }
  60.  
  61. UWORD GetNodeNum(struct List *lh, struct Node *node)
  62. {
  63.   struct Node *ln;
  64.   UWORD i;
  65.  
  66.   for (i = 0, ln = lh->lh_Head; ln != node; ln = ln->ln_Succ, i++)
  67.     ;
  68.   return i;
  69. }
  70.  
  71. //  ok now for the bitchy list stuff..
  72.  
  73. void FreeNameList(struct List *FileList)
  74. {
  75.   struct Node *Current,*Next;
  76.  
  77.   if (Current=FileList->lh_Head)
  78.   {
  79.     while (FileList->lh_Head->ln_Succ)
  80.     {
  81.       Next=Current->ln_Succ;
  82.       if (Current->ln_Name)
  83.       {
  84.         free(Current->ln_Name);
  85.       }
  86.       Remove(Current);
  87.       FreeMem(Current,sizeof(struct Node));
  88.       Current=Next;
  89.     }
  90.   }
  91. }
  92.  
  93. struct Node *NewNameNode(struct List *NameList,char *str)
  94. {
  95.   struct Node *new=NULL;
  96.  
  97.   if (new=AllocMem(sizeof(struct Node),MEMF_PUBLIC))
  98.   {
  99.     new->ln_Name=strdup(str);
  100.     AddTail(NameList,new);
  101.   }
  102.   return(new);
  103. }
  104.  
  105. void FreeNameNode(struct Node *node)
  106. {
  107.   if (node->ln_Name)
  108.   {
  109.     free(node->ln_Name);
  110.   }
  111. }
  112.  
  113.  
  114. void FreeTypeNode(struct TypeNode *new)
  115. {
  116.   if (new->nameplist)
  117.   {
  118.     FreeNameList(new->nameplist);
  119.     FreeMem(new->nameplist,sizeof (struct List));
  120.   }
  121.   if (new->fileplist)
  122.   {
  123.     FreeNameList(new->fileplist);
  124.     FreeMem(new->fileplist,sizeof (struct List));
  125.   }
  126.   if (new->IconName) free(new->IconName);
  127.   FreeNameNode(&new->typenode);
  128. }
  129.  
  130. void FreeTypeList(struct List *TypeList)
  131. {
  132.   struct TypeNode *Current,*Next;
  133.  
  134.   if (Current=(struct TypeNode*)TypeList->lh_Head)
  135.   {
  136.     while (TypeList->lh_Head->ln_Succ)
  137.     {
  138.       Next=(struct TypeNode *)Current->typenode.ln_Succ;
  139.       FreeTypeNode(Current);
  140.       Remove((struct Node*) Current);
  141.       FreeMem(Current,sizeof(struct TypeNode));
  142.       Current=Next;
  143.     }
  144.   }
  145. }
  146.  
  147.  
  148. short AllocTypeNode(struct TypeNode *new)
  149. {
  150.   short retval=FALSE;
  151.   if (new->nameplist=AllocMem(sizeof(struct List),MEMF_ANY))
  152.   {
  153.     NewList(new->nameplist);
  154.     if (new->fileplist=AllocMem(sizeof(struct List),MEMF_ANY))
  155.     {
  156.       NewList(new->fileplist);
  157.       if (new->IconName=malloc(256))
  158.       {
  159.         new->IconName[0]='\0';
  160.         retval=TRUE;
  161.       }
  162.     }
  163.   }
  164.   return (retval);
  165. }
  166.  
  167. struct TypeNode *NewTypeNode(struct List *TypeList,char *str)
  168. {
  169.   struct TypeNode *new;
  170.  
  171.   if (new=AllocMem(sizeof(struct TypeNode),MEMF_PUBLIC))
  172.   {
  173.  
  174.     new->typenode.ln_Name=strdup(str); // I should REALLY error check this line.
  175.     new->nameplist=NULL; // initialize structure..
  176.     new->fileplist=NULL;
  177.     new->IconName=NULL;
  178.     new->RunInfo=0;
  179.  
  180.     if (AllocTypeNode(new))
  181.     {
  182.       AddTail(TypeList,(struct Node *)new);
  183.     }
  184.     else
  185.     {
  186.       FreeTypeNode(new);
  187.       FreeMem(new,sizeof(struct TypeNode));
  188.       new=NULL;
  189.     }
  190.   }
  191.   return(new); // return NULL for fail or the pointer to the new structure..
  192. }
  193.  
  194. void SetDefault( void )
  195. {
  196.   struct TypeNode *mytn;
  197.   struct Node *mynode;
  198.   struct List *namelist;
  199.  
  200.   mytn=NewTypeNode(typelist,"Picture");
  201.   if (mytn)
  202.   {
  203.     mytn->IconName="ENV:Sys/Def_Misc.info";
  204.     namelist=mytn->nameplist;
  205.     mynode=NewNameNode(namelist,"#?.PIC");
  206.     mynode=NewNameNode(namelist,"#?.GIF");
  207.     mynode=NewNameNode(namelist,"#?.JPG");
  208.     mynode=NewNameNode(namelist,"#?.JPEG");
  209.     mynode=NewNameNode(namelist,"#?.IFF");
  210.     mynode=NewNameNode(namelist,"#?.ILBM");
  211.     namelist=mytn->fileplist;
  212.     mynode=NewNameNode(namelist,"FORM????ILBM#?");
  213.     mynode=NewNameNode(namelist,"GIF#?");
  214.   }
  215.   mytn=NewTypeNode(typelist,"Source Code");
  216.   if (mytn)
  217.   {
  218.     mytn->IconName="ENV:Sys/def_Source.info";
  219.     namelist=mytn->nameplist;
  220.     mynode=NewNameNode(namelist,"#?.c");
  221.     mynode=NewNameNode(namelist,"#?.s");
  222.     mynode=NewNameNode(namelist,"#?.i");
  223.     mynode=NewNameNode(namelist,"#?.h");
  224.     mynode=NewNameNode(namelist,"#?.p");
  225.     mynode=NewNameNode(namelist,"#?.pas");
  226.   }
  227. }
  228.  
  229. short SetUp( void )
  230. {
  231.   short retval=FALSE;
  232.  
  233.   if (ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))
  234.   {
  235.     if (typelist=AllocMem(sizeof(struct List),MEMF_ANY))
  236.     {
  237.       NewList(typelist);
  238.       if(IconBase = OpenLibrary("icon.library",37))
  239.       {
  240. #ifdef KS20
  241.         if (WorkbenchBase=OpenLibrary("workbench.library",37))
  242. #endif
  243. #ifdef KS30
  244.         if (WorkbenchBase=OpenLibrary("workbench.library",39))
  245. #endif
  246.         {
  247.           if (ascport=CreateMsgPort())
  248.           {
  249.             if (filereq = rtAllocRequestA (RT_FILEREQ, NULL))
  250.             {
  251.               retval=TRUE;
  252.             }
  253.             else rtEZRequest("Could not allocate requester",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  254.           }
  255.           else rtEZRequest("Could Not Create Message Port!",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  256.         }
  257. #ifdef KS20
  258.         else rtEZRequest("You Need workbench.library v37+!",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  259. #endif
  260. #ifdef KS30
  261.         else rtEZRequest("You Need workbench.library v39+!",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  262. #endif
  263.       }
  264.       else rtEZRequest("You Need icon.library v37!",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  265.     }
  266.     else rtEZRequest("Unable to alloc memory!",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  267.   }
  268.   return(retval);
  269. }
  270.  
  271. void KillAppIcon( void )
  272. {
  273.   if (appicon) RemoveAppIcon(appicon);
  274.   if (ascport)
  275.   {
  276.     while(appmsg=(struct AppMessage *)GetMsg(ascport))
  277.         ReplyMsg((struct Message *)appmsg);
  278.   }
  279. }
  280.  
  281. void CleanUp( void )
  282. {
  283.   KillAppIcon();
  284.   if ( ascport       ) DeleteMsgPort(ascport);
  285.   if ( dobj          ) FreeDiskObject(dobj);
  286.   if ( WorkbenchBase ) CloseLibrary(WorkbenchBase);
  287.   if ( IconBase      ) CloseLibrary(IconBase);
  288.   if ( filereq       ) rtFreeRequest (filereq);
  289.   if ( ReqToolsBase  ) CloseLibrary ((struct Library *)ReqToolsBase);
  290.   if ( typelist      )
  291.   {
  292.     FreeTypeList(typelist);
  293.     FreeMem(typelist,sizeof(struct List));
  294.   }
  295. }
  296.  
  297. short OpenAppIcon( void )
  298. {
  299.   short retval=FALSE;
  300.   if ((dobj=GetDiskObject(iconname))==NULL)
  301.   {
  302.     sprintf(msgstr,"Could Not Open \"%s.info\" to create the AppIcon\n"
  303.                    "You might still have to rename associate, see the docs!",iconname);
  304.     rtEZRequest(msgstr,okstr,NULL,(struct TagItem *)&reqtags,NULL);
  305.   }
  306.   else
  307.   {
  308.     dobj->do_Type=NULL; // set to null for appicon..,
  309.     // temp fix until loadprefs has been written..
  310.     dobj->do_CurrentX=TTInt(dobj,"ICONX",NO_ICON_POSITION);
  311.     dobj->do_CurrentY=TTInt(dobj,"ICONY",NO_ICON_POSITION);
  312.     // wb don't like only one of the coords set.. :-)
  313.     if (dobj->do_CurrentX >= 0 && dobj->do_CurrentY == NO_ICON_POSITION) dobj->do_CurrentY=1;
  314.     if (dobj->do_CurrentY >= 0 && dobj->do_CurrentX == NO_ICON_POSITION) dobj->do_CurrentX=1;
  315.     if ((appicon=AddAppIconA(0L,0L,"Associate",ascport,NULL,dobj,NULL))==NULL)
  316.     {
  317.       rtEZRequest("Could Not Create App Icon!",okstr,NULL,(struct TagItem *)&reqtags,NULL);
  318.     }
  319.     else
  320.     {
  321.       retval=TRUE;
  322.     }
  323.   }
  324.   return(retval);
  325. }
  326.  
  327. void DoOptions( void )
  328. {
  329.   if (!SetupScreen())
  330.   {
  331.     if (!OpenAssociateWindow())
  332.     {
  333.   #ifdef KS20
  334.       LastTypeClicked=-1;
  335.   #endif
  336.       GT_SetGadgetAttrs(AssociateGadgets[GD_TypeList], AssociateWnd, NULL,
  337.                         GTLV_Labels, (ULONG)typelist,
  338.                         GTLV_Selected,0,
  339.                         TAG_END);
  340.       UpdateLists();
  341.       do
  342.       {
  343.         Wait (1 << AssociateWnd->UserPort->mp_SigBit);
  344.       } while (HandleAssociateIDCMP());
  345.       CloseAssociateWindow();
  346.     }
  347.     CloseDownScreen();
  348.   }
  349. }
  350.  
  351. void CreateIcon(char *filename, struct TypeNode *tnode)
  352. {
  353.   char *iconname;
  354.   struct DiskObject *icn,*org=NULL;
  355.   int offset,orgoffset,newoffset;
  356.   char **ttypes=NULL,**oldttypes,*str;
  357.   BOOL cancel=FALSE;
  358.   iconname=strdup(tnode->IconName);
  359.   removeinfo(iconname);
  360.   if (icn=GetDiskObject(iconname))
  361.   {
  362.     icn->do_CurrentX=NO_ICON_POSITION;
  363.     icn->do_CurrentY=NO_ICON_POSITION;
  364.  
  365. // Added this next bit for v1.3
  366.  
  367.     if (org=GetDiskObject(filename))
  368.     {
  369.       if (org->do_ToolTypes && org->do_ToolTypes[0])
  370.       {
  371.         offset=0;
  372.         while (icn->do_ToolTypes[offset]) offset++;
  373.  
  374.         orgoffset=0;
  375.         while (org->do_ToolTypes[orgoffset]) orgoffset++;
  376.  
  377.         // save old pointer
  378.         oldttypes=icn->do_ToolTypes;
  379.  
  380.         if (str=AllocVec(strlen(filename)+40,MEMF_CLEAR))
  381.         {
  382.           sprintf(str,"An Icon Already Exists for file\n\"%s\"",filename);
  383.  
  384.           if (!ttall) ttallval=rtEZRequest(str,"_Add New ToolTypes|_Old ToolTypes Only|_New ToolTypes Only|_Skip",NULL,(struct TagItem *)&reqtags,NULL);
  385.           switch (ttallval)
  386.           {
  387.             case 1:
  388.  
  389.               if (ttypes=(char **)AllocVec((1+offset+orgoffset)*sizeof(LONG),MEMF_PUBLIC|MEMF_CLEAR))
  390.               {
  391.                 newoffset=0;
  392.  
  393.                 offset=0;
  394.                 while (icn->do_ToolTypes[offset])
  395.                 {
  396.                   ttypes[newoffset]=icn->do_ToolTypes[offset];
  397.                   offset++;
  398.                   newoffset++;
  399.                 }
  400.                 offset=0;
  401.                 while (org->do_ToolTypes[offset])
  402.                 {
  403.                   ttypes[newoffset]=org->do_ToolTypes[offset];
  404.                   offset++;
  405.                   newoffset++;
  406.                 }
  407.                 icn->do_ToolTypes=ttypes;
  408.               }
  409.               break;
  410.             case 2:
  411.               icn->do_ToolTypes=org->do_ToolTypes;
  412.               break;
  413. /*
  414.             case 3:
  415.               break;
  416. */
  417.             case 0:
  418.               cancel=TRUE;
  419.               break;
  420.           }
  421.           FreeVec(str);
  422.           if ((!cancel) && (IconsToProcess>0) && (!ttall))
  423.           {
  424.             if (rtEZRequest("Repeat for remaining icons ?","_Yes|_No",NULL,(struct TagItem *)&reqtags,NULL))
  425.             {
  426.               ttall=TRUE;
  427.             }
  428.           }
  429.         }
  430.       }
  431.  
  432.  
  433.     }
  434.  
  435.     if (!cancel) PutDiskObject(filename,icn);
  436.     if (ttypes)
  437.     {
  438.       icn->do_ToolTypes=oldttypes; // restore old pointer so AmigaDOS can free it..
  439.     }
  440.     FreeDiskObject(icn);
  441.     if (org)
  442.     {
  443.       FreeDiskObject(org);
  444.       if (ttypes) FreeVec(ttypes); // and we free our own set here..
  445.     }
  446.     if (tnode->RunInfo)
  447.     {
  448.       DoInfo(filename);
  449.     }
  450.     free(iconname);
  451.   }
  452.   else
  453.   {
  454.     rtEZRequest("Error: Can't open \"%s.info\"\nCheck your config",okstr,NULL,(struct TagItem *)&reqtags,iconname);
  455.   }
  456. }
  457.  
  458.  
  459. void PickType(char *filename)
  460. {
  461.   if (!SetupScreen())
  462.   {
  463.     if (!OpenPickWindow())
  464.     {
  465. #ifdef KS20
  466.       LastPickClicked=-1;
  467. #endif
  468.       GT_SetGadgetAttrs(PickGadgets[GD_PickType], PickWnd, NULL,
  469.                         GTLV_Labels, (ULONG)typelist,
  470.                         GTLV_Selected,0,
  471.                         TAG_END);
  472.       do
  473.       {
  474.         Wait (1 << PickWnd->UserPort->mp_SigBit);
  475.       } while (HandlePickIDCMP());
  476.  
  477.       // this is the only place temptnode is relied on between functions..
  478.       // so be carefull
  479.  
  480.       if (temptnode) // idcmp handling will set this to NULL if pickwindow is cancelled
  481.       {
  482.         CreateIcon(filename,temptnode);
  483.       }
  484.       ClosePickWindow();
  485.     }
  486.     CloseDownScreen();
  487.   }
  488. }
  489.  
  490. BOOL MatchFile(char *matchpattern, char *filename)
  491. {
  492.   BPTR File;
  493.   char *buffer;
  494.   LONG buflen;
  495.   BOOL retval=FALSE;
  496.   char match[256];
  497.   short loop;
  498.  
  499.   if (buffer=malloc(21))
  500.   {
  501.     if (File=Open(filename,MODE_OLDFILE))
  502.     {
  503.       if (buflen=Read(File,buffer,20)) // read 1st 20 bytes
  504.       {
  505.         for (loop=0;loop<buflen;loop++) // and check..
  506.         {
  507.           if (buffer[loop]==0) buffer[loop]='.'; // replace null bytes with .'s..
  508.         }
  509.         ParsePatternNoCase(matchpattern,match,255);
  510.         if (MatchPatternNoCase(match,buffer))
  511.         {
  512.           retval=TRUE;
  513.         }
  514.       }
  515.       Close(File);
  516.     }
  517.     free(buffer);
  518.   }
  519.   return(retval);
  520. }
  521.  
  522. LONG MatchOK(char *filename,struct TypeNode *tnode)
  523. {
  524.   LONG result;
  525.  
  526.   MatchType=tnode;
  527.  
  528.   if (!All)
  529.   {
  530.     sprintf(msgstr,"Matched file \"%s\" against\n"
  531.                    "type \"%s\"\n"
  532.                    "Do you want to use this type\n"
  533.                    "or continue searching ?",FilePart(filename),tnode->typenode.ln_Name);
  534.     result=rtEZRequest(msgstr,"OK!|_All First|All _This|Continue..",NULL,(struct TagItem *)&reqtags,NULL);
  535.     if (result==2) All=TRUE;
  536.     if (result==3)
  537.     {
  538.       All=TRUE;
  539.       AllThis=TRUE;
  540.     }
  541.     return(result);
  542.   }
  543.   else return(1);
  544. }
  545.  
  546. void DoIcon( char *filename)
  547. {
  548.   struct TypeNode *tnode;
  549.   struct Node *node;
  550.   char match[256];
  551.   char temp;
  552.   LONG Matched=FALSE;
  553.   char *iconname;
  554.   struct DiskObject *icn;
  555.  
  556.   temp=filename[strlen(filename)-1];
  557.   if (temp!='/' && temp!=':')
  558.   {
  559.     if (!AllThis)
  560.     {
  561.       for (tnode = (struct TypeNode *) typelist->lh_Head ; !Matched && tnode->typenode.ln_Succ ; tnode = (struct TypeNode *)tnode->typenode.ln_Succ)
  562.       {
  563.         for (node = tnode->nameplist->lh_Head ; !Matched && node->ln_Succ ; node = node->ln_Succ)
  564.         {
  565.           ParsePatternNoCase(node->ln_Name,match,255);
  566.           if (MatchPatternNoCase(match,FilePart(filename)))
  567.           {
  568.             Matched=MatchOK(filename,tnode);
  569.           }
  570.         }
  571.         if (!Matched)
  572.         {
  573.           for (node = tnode->fileplist->lh_Head ; !Matched && node->ln_Succ ; node = node->ln_Succ)
  574.           {
  575.             if (MatchFile(node->ln_Name,filename))
  576.             {
  577.               Matched=MatchOK(filename,tnode);
  578.             }
  579.           }
  580.         }
  581.       }
  582.     }
  583.     else
  584.     {
  585.       Matched=TRUE;
  586.     }
  587.  
  588.     if (Matched)
  589.     {
  590.       CreateIcon(filename,MatchType);
  591.     }
  592.     else
  593.     {
  594.       sprintf(msgstr,"Sorry, I could not find a match\n"
  595.                      "for the file \"%s\"",FilePart(filename));
  596.       if (rtEZRequest(msgstr,"Select A Type|Skip",NULL,(struct TagItem *)&reqtags,NULL))
  597.       {
  598.         PickType(filename);
  599.       }
  600.     }
  601.   }
  602.   else
  603.   {
  604.     if (iconname=strdup(filename))
  605.     {
  606.       sprintf(msgstr,"Copy Default %s Icon \nto %s ?",temp == '/' ? "Drawer" : "Disk",iconname);
  607.       if (temp=='/')
  608.       {
  609.  
  610.         if ((doalldrawers==2) || (doalldrawers=rtEZRequest(msgstr,"Yes|All|Cancel",NULL,(struct TagItem *)&reqtags,NULL)))
  611.         {
  612.           iconname[strlen(iconname)-1]=0;
  613.           if (icn=GetDiskObject("env:sys/def_drawer"))
  614.           {
  615.             icn->do_CurrentX=NO_ICON_POSITION;
  616.             icn->do_CurrentY=NO_ICON_POSITION;
  617.             PutDiskObject(iconname,icn);
  618.             FreeDiskObject(icn);
  619.           }
  620.         }
  621.       }
  622.       else // disk..
  623.       {
  624.         if (rtEZRequest(msgstr,"Yes|Cancel",NULL,(struct TagItem *)&reqtags,NULL))
  625.         {
  626.           strcat(iconname,"disk");
  627.           if (icn=GetDiskObject("env:sys/def_disk"))
  628.           {
  629.             icn->do_CurrentX=NO_ICON_POSITION;
  630.             icn->do_CurrentY=NO_ICON_POSITION;
  631.             PutDiskObject(iconname,icn);
  632.             FreeDiskObject(icn);
  633.           }
  634.         }
  635.       }
  636.       free(iconname);
  637.     }
  638.   }
  639. }
  640.  
  641. void CheckMessages( void )
  642. {
  643.   short loop;
  644.   char *filename;
  645.  
  646.   All=FALSE;
  647.   AllThis=FALSE;
  648.   MatchType=NULL;
  649.   doalldrawers=0;
  650.   ttall=FALSE;
  651.   ttallval=0;
  652.   while(appmsg=(struct AppMessage *)GetMsg(ascport))
  653.   {
  654.     if(appmsg->am_NumArgs==0L)
  655.     {
  656.       DoOptions();
  657.     }
  658.     else
  659.     if(appmsg->am_NumArgs>0L) // could it be less than 0 ?
  660.     {
  661.       IconsToProcess=appmsg->am_NumArgs;
  662.       if (filename=malloc(256))
  663.       {
  664.         for(loop=0;loop<appmsg->am_NumArgs;loop++)
  665.         {
  666.           IconsToProcess--;
  667.           NameFromLock(appmsg->am_ArgList[loop].wa_Lock,filename,255);
  668.           AddPart(filename,appmsg->am_ArgList[loop].wa_Name,255);
  669.           DoIcon(filename);
  670.         }
  671.         free(filename);
  672.       }
  673.     }
  674.     ReplyMsg((struct Message *)appmsg);
  675.   }
  676. }
  677.  
  678. void stripcr( char *str)
  679. {
  680.   if (str[strlen(str)-1]=='\n') str[strlen(str)-1]=0;
  681.   if (str[strlen(str)-1]=='\r') str[strlen(str)-1]=0;
  682. }
  683.  
  684. void SavePrefs( void )
  685. {
  686.   FILE *File;
  687.   struct TypeNode *tnode;
  688.   struct Node *node;
  689.  
  690.   if (File=fopen(cfgname,"w"))
  691.   {
  692.     for (tnode = (struct TypeNode *) typelist->lh_Head ;tnode->typenode.ln_Succ ; tnode = (struct TypeNode *)tnode->typenode.ln_Succ)
  693.     {
  694.       fprintf(File,"|STARTLIST\n"
  695.                    "%s\n" // node name
  696.                    "%s\n" // icon name
  697.                    "%d\n", // RunInfo
  698.                    tnode->typenode.ln_Name,tnode->IconName,tnode->RunInfo);
  699.       if (tnode->nameplist->lh_Head->ln_Succ)
  700.       {
  701.         fprintf(File,"|NAMEPATTERN\n");
  702.         for (node = tnode->nameplist->lh_Head ;node->ln_Succ ; node = node->ln_Succ)
  703.         {
  704.           fprintf(File,"%s\n",node->ln_Name);
  705.         }
  706.       }
  707.       if (tnode->fileplist->lh_Head->ln_Succ)
  708.       {
  709.         fprintf(File,"|FILEPATTERN\n");
  710.         for (node = tnode->fileplist->lh_Head ;node->ln_Succ ; node = node->ln_Succ)
  711.         {
  712.           fprintf(File,"%s\n",node->ln_Name);
  713.         }
  714.       }
  715.       fprintf(File,"|ENDLIST\n");
  716.     }
  717.     fclose(File);
  718.   }
  719. }
  720.  
  721. short LoadPrefs( void )
  722. {
  723.   FILE *File;
  724.   char str[256];
  725.   char what='x';
  726.  
  727.   // load prefs.  return 1 if OK 0 otherwise..
  728.  
  729.   if (File=fopen(cfgname,"r"))
  730.   {
  731.     while (!feof(File))
  732.     {
  733.       fgets(str,255,File);
  734.       if (!feof(File))
  735.       {
  736.         stripcr(str);
  737.  
  738.         // ok, so i could use a "flag" here :-) buy hey! what do i care..
  739.  
  740.         if (stricmp(str,"|ENDLIST")==0)
  741.         {
  742.           what='x';
  743.         }
  744.         else
  745.         {
  746.           if (stricmp(str,"|NAMEPATTERN")==0)
  747.           {
  748.             what='n';
  749.           }
  750.           else
  751.           {
  752.             if (stricmp(str,"|FILEPATTERN")==0)
  753.             {
  754.               what='f';
  755.             }
  756.             else
  757.             {
  758.               if (stricmp(str,"|STARTLIST")==0)
  759.               {
  760.                 what='l';
  761.               }
  762.               else
  763.               {
  764.                 if (what!='x')
  765.                 {
  766.                   switch(what)
  767.                   {
  768.                     case 'n':
  769.                       NewNameNode(temptnode->nameplist,str);
  770.                       break;
  771.                     case 'f':
  772.                       NewNameNode(temptnode->fileplist,str);
  773.                       break;
  774.                     case 'r':
  775.                       sscanf(str,"%d",&temptnode->RunInfo);
  776.                       what='x';
  777.                       break;
  778.                     case 'i':
  779.                       strncpy(temptnode->IconName,str,255); // limited..
  780.                       what='r';
  781.                       break;
  782.                     case 'l':
  783.                       temptnode=NewTypeNode(typelist,str);
  784.                       what='i';
  785.                       break;
  786.                   }
  787.                 }
  788.               }
  789.             }
  790.           }
  791.         }
  792.       }
  793.     }
  794.     fclose(File);
  795.     return(1);
  796.   }
  797.   return(0);
  798. }
  799.  
  800. void main(int argc,char *argv[])
  801. {
  802.   WB2CLI((struct WBStartup *)argv,4000,DOSBase);  /* so you get the default path! */
  803.   SetUp();
  804.   if (!LoadPrefs())
  805.   {
  806.     SetDefault();
  807.     DoOptions();
  808.   }
  809.  
  810.   if (argc==2)
  811.   {
  812.     DoIcon(argv[1]);
  813.   }
  814.   else
  815.   {
  816.     if (OpenAppIcon())
  817.     {
  818.       while (!done)
  819.       {
  820.         WaitPort(ascport);
  821.         CheckMessages();
  822.       }
  823.     }
  824.     else
  825.     {
  826.       rtEZRequest("Could not create an AppIcon!","Quit!",NULL,(struct TagItem *)&reqtags,NULL);
  827.     }
  828.   }
  829.   CleanUp();
  830. }
  831.